﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using BLL;
using System.Configuration;

public partial class Crm_BoardsList : BaseCrm
{
    public int fid = 0;
    List<Dukey.Model.Channel> list = BLL.Channel.instance.GetList("");
    private static string dal = ConfigurationManager.AppSettings["DAL"];
    public string typestr = "accessdata";
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
       
        string xpath = "";
        int fid=0;
        if (StringHelper.isNum(Request.QueryString["fid"]))
        {
            fid = int.Parse(Request.QueryString["fid"]);
            var fboard = list.Find(item => item.bid == fid);
            if (fboard != null)
            {
                xpath = fboard.xpath;
            }
            object px=DataBaseHelper.instance.GetSingle("Channel","px","fid="+fid,"px desc");
            px=(px==null?0:px);
            
            Hashtable hs1=new Hashtable();
            hs1.Add("cname","新版块");
            hs1.Add("ename","newboard");
            hs1.Add("fid",fid);
            hs1.Add("px", Convert.ToInt32(px) + 1);
            BLL.DataBaseHelper.instance.Insert(hs1, "Channel");
            int id = BLL.DataBaseHelper.instance.GetMaxID("bid","Channel");
            if (id > 0)
            {
                Hashtable hs = new Hashtable();
                //Response.Write(xpath);
                //Common.MessageBox.Show(this,xpath);
                hs.Add("xpath", xpath + "," + id);
                hs.Add("linkto", "/show.ashx?fn=list&bid=" + id);
                hs.Add("linktohtml", "/html/" + id + ".html");
                DataBaseHelper.instance.Update(hs, "Channel", "bid=" + id);
            }
        }
        else
        {
            object px = DataBaseHelper.instance.GetSingle("Channel", "px", "fid=" + fid, "px desc");
            px = (px == null ? 0 : px);            
            Hashtable hs1 = new Hashtable();
            hs1.Add("cname", "新版块");
            hs1.Add("ename", "newboard");
            hs1.Add("fid", fid);
            hs1.Add("px", Convert.ToInt32(px) + 1);
            BLL.DataBaseHelper.instance.Insert(hs1, "Channel");
            int id = BLL.DataBaseHelper.instance.GetMaxID("bid", "Channel");
            if (id > 0)
            {
                Hashtable hs = new Hashtable();
                hs.Add("xpath", id);
                hs.Add("linktohtml", "/html/" + id + ".html");
                hs.Add("linkto", "/show.ashx?fn=list&bid=" + id);
                DataBaseHelper.instance.Update(hs, "Channel", "bid=" + id);
            }
        }

        gvList.DataSourceID = typestr;
    }

    string str = "";
    public string GetNavLink(int id)
    {
        if (list != null)
        {
            Dukey.Model.Channel model = list.Find(item => (item.bid == id));
            if (model != null && model.fid == 0)
            {
                str = string.Format("<a style=\"color:#fff;\" href='BoardsList.aspx?fid={0}'>{1}</a>&gt;&gt;", model.bid, model.cname) + str;
                return str.ToString();
            }
            else if (model != null && model.fid != 0)
            {
                str = string.Format("<a style=\"color:#fff;\" href='BoardsList.aspx?fid={0}'>{1}</a>&gt;&gt;", model.bid, model.cname) + str;
                return GetNavLink(model.fid);
            }
            else
            {
                return "";
            }
        }
        else
        {
            return "";
        }

    }
}
